import User, { UserInfo } from "../../../utils/user";
import { JoinActivity, CancelJoin, GetJoinData, GetJoinAccount, GetStudentData } from "../../../utils/request/api";
import msg from "../../../utils/msg";
var user = new User;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    info: user.getInfo() as UserInfo,
    student_data:
    {
      title: '' as string,
      s_name: '' as string,
      s_code: '' as string,
      c_name: '' as string,
    },
    content: {
      id: 0 as number,
      title: '' as string,
      content: '' as string,
      people: '' as string,
      place: '' as string,
      start_time: '' as string,
      end_time: '' as string,
    },
    id: 0 as number,
    isJoin: false,
    account: 0 as number,
    filepath: '' as string,
    user: user,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {
    var id = options.index;
    let data = options.data || "";
    let details = JSON.parse(data);
    this.setData({
      id: id,
      content: details
    });
    GetJoinData({ s_code: this.data.info.code, a_id: this.data.content.id }).then((res: any) => {
      this.setData({
        isJoin: res.data.data,
      })
    });
    GetJoinAccount({ a_id: this.data.content.id }).then((res: any) => {
      this.setData({
        account: res.data.data,
      })
    });
  },

  //参与活动
  onJoin: async function () {
    await JoinActivity({ title: this.data.content.title, a_id: this.data.content.id, s_name: this.data.info.name, s_code: this.data.info.code, class_id: this.data.info.class_id, c_name: this.data.info.className }).then(() => {
      msg.show('参与成功', '再次点击取消参与');
      this.setData({
        isJoin: true
      });
    });
  },

  //取消参加
  onCancelJoin: async function () {
    await CancelJoin({ s_code: this.data.info.code, a_id: this.data.content.id }).then(() => {
      msg.show('取消成功', '点击即可参与');
      this.setData({
        isJoin: false
      });
    })
  },


  //返回操作
  onClickLeft() {
    wx.navigateBack();
  },

  //下载参与学生数据
  onStudentData: async function () {
    GetStudentData({ a_id: this.data.content.id }).then((res: any) => {
      this.setData({
        filepath: res.data.data
      })
    })
    // 下载文件
    wx.downloadFile({
      url: this.data.filepath, // 文件的远程地址
      success: function (res) {
        let tempFilePath = res.tempFilePath; // 下载到的临时文件路径

        // 保存文件到本地
        //wx.saveFileToDisk({
        //  filePath: tempFilePath,
        //  success: function (res:any) {
        //    var savedFilePath = res.savedFilePath; // 保存到本地的文件路径
        //    console.log('文件保存成功，路径：', savedFilePath);

        // 打开文件
        wx.openDocument({
          filePath: tempFilePath,
          success: function (res) {
            console.log('打开文件成功',res);
          },
          fail: function (res) {
            console.log('打开文件失败', res);
          }
        });
      },
      fail: function (res: any) {
        console.log('保存文件失败', res);
      }
    });
  },
  //fail: function (res) {
  //  console.log('下载文件失败', res);
  //}
  //});
  //},
})